package com.bizihang.wiremock;

import com.github.tomakehurst.wiremock.client.WireMock;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.MediaType;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author 毕子航 951755883@qq.com
 * @date 2018/08/15
 */
public class MockServer {
	@Before
	public void setup() {
		WireMock.configureFor(8018);
		WireMock.removeAllMappings();
	}

	@Test
	public void getOneOrder() {
		MockServer.getMock("/orders/1", "01");
	}

	private static void getMock(String url, String fileName) {
		String s = null;
		try {
			s = FileCopyUtils.copyToString(new FileReader(new ClassPathResource("mock/response/" + fileName + ".json").getFile()));
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (StringUtils.hasText(s)) {
			WireMock.stubFor(WireMock
					.get(WireMock
							.urlPathEqualTo(url))
					.willReturn(WireMock.okForContentType(MediaType.APPLICATION_JSON_UTF8_VALUE, s)));
		}
	}
}
